package com.example.financecollegeorderdish.db;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.TypeConverters;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

import com.example.financecollegeorderdish.db.bean.DishBean;
import com.example.financecollegeorderdish.db.dao.DishBeanDao;
import com.wuhao.lib_common.configure.BaseConfig;


/**
 * @author zhangjianhao
 * @date 2019-11-25
 */
@Database(entities = {DishBean.class},
        version = 1)
public abstract class AppDatabase extends RoomDatabase {
    /**
     * 获取Dao对象
     *
     * @return
     */

    public abstract DishBeanDao dishBeanDao();

    private static volatile AppDatabase instance;

    public static AppDatabase getInstance(Context context) {
        if (instance == null) {
            synchronized (AppDatabase.class) {
                if (instance == null) {
                    instance = Room.databaseBuilder(context, AppDatabase.class, BaseConfig.DB_NAME)
//                            .addMigrations(MIGRATION_1_2)
                            .build();
                }
            }
        }
        return instance;
    }

    /**
     * 升级version 1 至 version 2
     */
    static Migration MIGRATION_1_2 = new Migration(1, 2) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {
            // 为旧表添加新的字段
        }
    };
}
